{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 类属性和实例属性\n",
    "\n",
    "1. 类属性直接在类中定义，不在接口函数中定义；\n",
    "2. 类属性也可以动态绑定；\n",
    "3. 类属性由所有实例共享；\n",
    "4. 类属性可以由`类名.属性`调用；\n",
    "5. 实例属性在接口函数中由`self.属性`定义；\n",
    "6. 实例属性也可以由实例动态绑定；\n",
    "7. 类属性与实例属性不可同名, 否则，调用`实例.属性`,优先调用实例属性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student.Count:  1\n",
      "ming.Count:  1\n",
      "Student.score:  0\n",
      "ming.score:  98\n",
      "ming.score:  0\n",
      "Student.Count:  2\n",
      "ming.Count:  2\n",
      "Student.score:  0\n",
      "hua.score:  95\n"
     ]
    }
   ],
   "source": [
    "class Student(object):\n",
    "    Count = 0 # 定义类属性\n",
    "    score = 0 # 定义类属性\n",
    "    \n",
    "    def __init__(self, name, score):\n",
    "        self.name = name   # 定义实例属性\n",
    "        self.score = score\n",
    "        Student.Count += 1\n",
    "        \n",
    "ming = Student('ming', 98)\n",
    "print(\"Student.Count: \", Student.Count) # 访问类属性\n",
    "print(\"ming.Count: \", ming.Count)       # 用实例访问类属性\n",
    "print(\"Student.score: \", Student.score) # 访问类属性\n",
    "print(\"ming.score: \", ming.score) # 访问实例属性\n",
    "del ming.score # 删除实例属性score\n",
    "print(\"ming.score: \", ming.score) # 会调用类score\n",
    "\n",
    "hua = Student('hua', 95)\n",
    "print(\"Student.Count: \", Student.Count) # 访问类属性\n",
    "print(\"ming.Count: \", ming.Count)       # 访问类属性\n",
    "print(\"Student.score: \", Student.score) # 访问类属性\n",
    "print(\"hua.score: \", hua.score)         # 访问实例属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
